package com.cxy.datawarehouse.controller;


import com.cxy.datawarehouse.annotation.NotLogin;
import com.cxy.datawarehouse.biz.UserBiz;
import com.cxy.datawarehouse.entity.User;
import com.cxy.datawarehouse.response.Result;
import com.cxy.datawarehouse.voEntity.UserLoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags="用户模块")
@RestController
@Validated
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserBiz userBiz;


    @NotLogin
    @PostMapping(value = "/create")
    @ApiOperation("创建账号")
    public Result<String> create(@RequestBody User user) {
        userBiz.create(user);
        return Result.ok();
    }

    @NotLogin
    @PostMapping(value = "/loginByName")
    @ApiOperation(value = "账号密码登录")
    public Result<String> loginByName(@RequestBody UserLoginVo user) {
        String token = userBiz.loginByName(user);
        return Result.ok(token);
    }

    @GetMapping(value = "/getUserInfo/{token}")
    @ApiOperation(value = "获取用户信息")
    public Result<User> loginByName(@PathVariable String token) {
        User userInfo = userBiz.getUserInfo(token);
        return Result.ok(userInfo);
    }

    @GetMapping(value = "/logout/{token}")
    @ApiOperation(value = "退出登录")
    public Result<Long> logout(@PathVariable String token) {
        userBiz.logout(token);
        return Result.ok();
    }
}
